<?php
namespace Little\Controller;
header("Content-type: text/html; charset=utf-8");
use Think\Controller;
//**********************个人中心****************************

class CenterController extends Controller{
    //展示
    public function index(){
        $code       = I('code');
        $user       = user_add($code,"id,flowers,covers,coupons_id,goods_id,back_img,vip_type,nickname,headimgurl,end_at,xian_nums");
        /*if(mb_strlen($user['nickname'],'utf8') >8){
            $user['nickname'] = mb_substr($user['nickname'],0,8,"UTF8");
        }*/
        //用户参加线下活动的次数
        if($user['xian_nums'] == -1){
            $user['cishu'] = "不限次数";
        }else{
            if($user['xian_nums']){
                $user['cishu'] = $user['xian_nums'];
            }else{
                $user['cishu'] = 0;
            }
        }
        //公司电话
        $company = sel("company","phone");
       /* //我的收藏
        $shou = M('collect_goods')->where("user_id = {$user['id']}")->count();
        if($shou<1){$shou = "0";}*/
        //优惠券
        $coupons                 = couponss($user['id']);
        /*$user['collect']        = $shou;*/
        $user['coupons']        = $coupons['counts'];
        $user['company_phone'] = $company['phone'];
        //我的消息
        $users_jiaoinfo = M('users_jiaoinfo')->where("bei_id = {$user['id']} and status = 1")->count();
        if(!$users_jiaoinfo){$users_jiaoinfo = 0;}
        $user['users_jiaoinfo'] = $users_jiaoinfo;
        //用户个人中心背景
        if($user['back_img']){
            $img = sel("users_backimg","img","id = {$user['back_img']}");
            $user['back_img'] = IMGS.$img['img'];
        }else{
            $img = sel("users_backimg","img");
            $user['back_img'] = IMGS.$img['img'];
        }
        //用户基本信息
        $jiinfo = sel("users_jiinfo","nick_name,avatar,shen,ren_info","user_id = {$user['id']}");
        if($jiinfo){
            $tian = 1;
            $user['shen']    = $jiinfo['shen'];
            if($jiinfo['avatar']){
                $user['avatar']   = IMGS.$jiinfo['avatar'];
            }else{
                $user['avatar']   = $user['headimgurl'];
            }
            if($jiinfo['nick_name']){
                $user['nickname'] = $jiinfo['nick_name'];
            }
        }else{
            $user['avatar']   = $user['headimgurl'];
            $tian = 2;
        }
        //会员信息
        if(($user['vip_type'] && ($user['end_at'] == -1) && $jiinfo['shen'] == 2) || ($user['vip_type'] && (time() <= $user['end_at']) && $jiinfo['shen'] == 2)){//判断用户vip是否到期 $user
            $vip  = sel("vips","tags,vip_type,end_date","id = {$user['vip_type']}");
            $vips = sel("vip_type","name","id = {$vip['vip_type']}");
            $user['vip'] = $vips['name'].$vip['tags'];
        }else{
           if($jiinfo){
               if($jiinfo['shen'] == 2){
                   $user['vip'] = "普通用户";
               }else if($jiinfo['shen'] == 3){
                   $user['vip'] = "认证失败";
                   $user['why'] = $jiinfo['ren_info'];
               }else{
                   $user['vip'] = "未认证";
               }
           }else{
               $user['vip'] = "待完善信息";
           }
        }
        //我的好友数
        $collect_cha = M("collect_cha")->where("user_id = {$user['id']}")->count();
        if(!$collect_cha){$collect_cha = 0;}
        $user['collect_cha'] = $collect_cha;
        //图标
        $icon = sels("icons", " * ","","id asc");
        foreach ($icon as $k => $v) {
            $icon[$k]['nav_icon'] = IMGS . $v['img'];
            $icon[$k]['nav_name'] = $v['name'];
            $icon[$k]['types']    = 1;
            //购物车 待付款  待发货 待收货  待评价
            if($v['id'] == 1){
                $count = M("cars")->where("user_id = {$user['id']}")->count();
            }else{
                $t = $v['id'] -1;
                $count = M("orders")->where("type = 1 and user_id = {$user['id']} and status = {$t}")->count();
            }
            $icon[$k]['numss'] = $count;
        }
        //积分
        //我的积分
        if ($user['covers'] < 1) {
            $user['covers'] = "0";
        }
        //收到的鲜花
        if(!$user['flowers']){$user['flowers'] = 0;}
        $url = JISHU;
        echo json_encode(array("info"=>$user,"icon" => $icon,"tian"=>$tian,"url"=>$url));
    }
    //用户资料设置详情
    public function zi_she(){
        $code    = I('code');
        $use     = users("users","id",$code);
        $user_id = $use['id'];
        $user    = join_one("users","a.id,a.back_img,b.avatar,b.img,b.nick_name,b.vedio",array("a left join __USERS_JIINFO__ b on a.id = b.user_id"),"a.id = {$user_id}");
        //用户个人中心背景
        if($user['back_img']){
            $img = sel("users_backimg","img","id = {$user['back_img']}");
            $user['back_img'] = IMGS.$img['img'];
        }else{
            $img = sel("users_backimg","img");
            $user['back_img'] = IMGS.$img['img'];
        }
        //用户基本信息
        if($user['avatar']){
            $user['avatar'] = IMGS.$user['avatar'];
        }else{
            $user['avatar'] = 1;
        }
        if($user['img']){
            $img = explode(",", $user['img']);
            if($img[0]){$user['img_one']   = IMGS.$img[0];}else{$user['img_one']   = 1;}
            if($img[1]){$user['img_two']   = IMGS.$img[1];}else{$user['img_two']   = 1;}
            if($img[2]){$user['img_three'] = IMGS.$img[2];}else{$user['img_three'] = 1;}
            if($img[3]){$user['img_four']  = IMGS.$img[3];}else{$user['img_four']  = 1;}
        }else{
            $user['img_one'] = 1;$user['img_two'] = 1;$user['img_three'] = 1;$user['img_four'] = 1;
        }
        //用户标签数组
        $arr = user_info();
        //用户基本信息
        $ji = sel("users_jiinfo","","user_id = {$user['id']}");
        if($ji){
            //标签
            if($ji['tags']){
                $ta = explode("/",$ji['tags']);
                $tags_b   = sel("user_tags","content","id=1");
                $tags_g   = sel("user_tags","content","id=2");
                $tags_bs   = explode("/",$tags_b['content']);
                $tags_gs   = explode("/",$tags_g['content']);
                $tagss = array_merge($tags_bs,$tags_gs);
                for ($i = 0;$i<count($tagss);$i++){
                    if(in_array($tagss[$i],$ta)){
                        $tags[$i]['choose'] = true;
                    }else{
                        $tags[$i]['choose'] = false;
                    }
                    $tags[$i]['name'] = $tagss[$i];
                }
                $arr['tags'] = $tags;
            }
            //休假情况
            if($ji['vacation']){
                $vacations = explode(",",$ji['vacation']);
                $week        = array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
                for ($i=0;$i<7;$i++){
                    if($i== 0){
                        $times = time();
                        $we    = $week[date("w",$times)]."(".date("Y/m/d",$times).")";
                        if(in_array($we,$vacations)){
                            $vacation[$i]['choose'] = true;
                        }else{
                            $vacation[$i]['choose'] = false;
                        }
                        $vacation[$i]['week'] = $we;
                    }else{
                        $times = strtotime(+$i.'day');
                        $we    = $week[date("w",$times)]."(".date("Y/m/d",$times).")";
                        if(in_array($we,$vacations)){
                            $vacation[$i]['choose'] = true;
                        }else{
                            $vacation[$i]['choose'] = false;
                        }
                        $vacation[$i]['week'] = $we;
                    }
                }
                $arr['week'] = $vacation;
            }
            //qq/wechat
            if($ji['qq']){
                $qq_wechat = array("qq"=>$ji['qq'],"wechat"=>$ji['wechat']);
            }else{$qq_wechat = 1;}
        }else{$ji = 1;$qq_wechat = 1;}
        //用户择偶条件
        $ze = sel("users_zeinfo","","user_id = {$user['id']}");
        if(!$ze){$ze = 1;}
        //性格标签 休假情况
        if($ji == 1){
            $vaca = 1;
            $ta   = 1;
        }else{
            if(!$ji['vacation']){
                $vaca = 1;
            }else{$vaca = 2;}
            if(!$ji['tags']){
                $ta   = 1;
            }else{$ta   = 2;}
        }
        if($user['vedio']){
            $user['vedio'] = IMGS.$user['vedio'];
        }else{$user['vedio'] = 1;}
        echo json_encode(array("info"=>$user,"arr"=>$arr,"ji"=>$ji,"ze"=>$ze,"qq_wechat"=>$qq_wechat,"vacation"=>$vaca,"tags"=>$ta));
    }
    //基本资料保存
    public function ji_cun(){
       /* $data["heights"]= $arr['heights'][$_GET['heights']];*/
        $phone = I('phone');
        if (preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
            $data['phone'] = $phone;
        } else {
            if (preg_match("/^([0-9]{3,4}-)?[0-9]{7,8}$/", $phone)) {
                $data['phone'] = $phone;
            } else {
                $data['phone'] = false;
            }
        }
        $code = I('code');
        $user = users("users","id",$code);
        //存储用户form_id
        $form_id = I('form_id');
        if($form_id){
            $form_id = form_id($form_id,$user['id']);
        }
       if($data['phone']){
           $data["xingqu"]= $_GET['xingqu'];
           $data["xingge"]= $_GET['xingge'];
           $data["heights"]= $_GET['heights'];
           $data["weights"]=$_GET['weights'];
           $data["education"]=I('education');
           $data["sex"]=I('sex');
           $data["home"]=$_GET['home'];
           $data["car"]=$_GET['car'];
           $data["marriage"]=$_GET['marriage'];
           $data["child"]=$_GET['child'];
           $data["moneys"]=$_GET['moneys'];
           $data["smoking"]=$_GET['smoking'];
           $data["drinking"]=$_GET['drinking'];
           $data["marry_date"]=$_GET['marry_date'];
           $data['id_card'] = I('id_card');
           $data['address'] = I('address');
           $data['age'] = I('age');
           $data['name'] = I('name');
           $data['nick_name'] = I('nick_name');
           $data['jobs'] = I('jobs');
           $data['place'] = I('place');
           $data['province'] = I('province');
           $data['city'] = I('city');
           $data['area'] = I('area');

           //判断用户是修改还是新增
           $pan = sel("users_jiinfo","id","user_id = {$user['id']}");
           if($pan){
               $data['up_at'] = time();
               $res = up_one("users_jiinfo","id = {$pan['id']}",$data);
           }else{
               $data['user_id'] = $user['id'];
               $data['create_at'] = time();
               $res = ins("users_jiinfo",$data);
           }
           $arr = array("xingqu"=>$data['xingqu'],"xingge"=>$data['xingge'],"place"=>$data['place'],"address"=>$data['address'],"phone"=>$data['phone'],"name"=>$data['name'],"nick_name"=>$data['nick_name'],"jobs"=>$data['jobs'],"id_card"=>$data['id_card']);
           if($res){echo json_encode(array("info"=>1,"ji"=>$arr));}else{echo json_encode(array("info"=>2,"ji"=>$arr));}
       }else{
           echo 3;
       }
    }
    //择偶信息资料保存
    public function ze_cun(){
        $data["leixing"]= $_GET['leixing'];
        $data["xuanyan"]= $_GET['xuanyan'];
        $data["heights"]= $_GET['heights'];
        $data["weights"]=$_GET['weights'];
        $data["education"]=$_GET['education'];
        $data["home"]=$_GET['home'];
        $data["car"]=$_GET['car'];
        $data["marriage"]=$_GET['marriage'];
        $data["child"]=$_GET['child'];
        $data["moneys"]=$_GET['moneys'];
        $data["smoking"]=$_GET['smoking'];
        $data["drinking"]=$_GET['drinking'];
        $data['address'] = I('address');
        $data['province'] = I('province');
        $data['city'] = I('city');
        $data['area'] = I('area');

        $code = I('code');
        $user = users("users","id",$code);
        //存储用户form_id
        $form_id = I('form_id');
        if($form_id){
            $form_id = form_id($form_id,$user['id']);
        }
        //判断用户是修改还是新增
        $pan = sel("users_zeinfo","id","user_id = {$user['id']}");
        if($pan){
            $data['up_at'] = time();
            $res = up_one("users_zeinfo","id = {$pan['id']}",$data);
        }else{
            $data['user_id']   = $user['id'];
            $data['create_at'] = time();
            $res = ins("users_zeinfo",$data);
        }
        if($res){echo json_encode(array("info"=>1,"ze"=>array("address"=>$data['address'],"leixing"=>$data['leixing'],"xuanyan"=>$data['xuanyan'])));}else{echo json_encode(array("info"=>2,"ze"=>array("address"=>$data['address'],"leixing"=>$data['leixing'],"xuanyan"=>$data['xuanyan'])));}
    }
    //qq/wechat
    public function qq_wechat(){
        $data['qq'] = I('qq');
        $data['wechat'] = I('wechat');

        $code = I('code');
        $user = users("users","id",$code);
        //存储用户form_id
        $form_id = I('form_id');
        if($form_id){
            $form_id = form_id($form_id,$user['id']);
        }
        //判断用户是修改还是新增
        $pan = sel("users_jiinfo","id","user_id = {$user['id']}");
        if($pan){
            $data['up_at'] = time();
            $res = up_one("users_jiinfo","id = {$pan['id']}",$data);
        }else{
            $data['user_id']   = $user['id'];
            $data['create_at'] = time();
            $res = ins("users_jiinfo",$data);
        }
        $arr = array("qq"=>$data['qq'],"wechat"=>$data['wechat']);
        if($res){echo json_encode(array("info"=>1,"qq_wechat"=>$arr));}else{echo json_encode(array("info"=>2,"qq_wechat"=>$arr));}
    }
    //用户标签
    public function tags(){
        $tags = json_decode($_GET['tags']);
        $data['tags'] = implode("/",$tags);
        $code = I('code');
        $user = users("users","id",$code);
        //存储用户form_id
        $form_id = I('form_id');
        if($form_id){
            $form_id = form_id($form_id,$user['id']);
        }
        //判断用户是修改还是新增
        $pan = sel("users_jiinfo","id","user_id = {$user['id']}");
        if($pan){
            $data['up_at'] = time();
            $res = up_one("users_jiinfo","id = {$pan['id']}",$data);
        }else{
            $data['user_id']   = $user['id'];
            $data['create_at'] = time();
            $res = ins("users_jiinfo",$data);
        }
        if($res){echo 1;}else{echo 2;}
    }
    //用户休假情况
    public function vacation(){
        $vacation = json_decode($_GET['vacation']);
        $data['vacation'] = implode(",",$vacation);
        $code = I('code');
        $user = users("users","id",$code);
        //存储用户form_id
        $form_id = I('form_id');
        if($form_id){
            $form_id = form_id($form_id,$user['id']);
        }
        //判断用户是修改还是新增
        $pan = sel("users_jiinfo","id","user_id = {$user['id']}");
        if($pan){
            $data['up_at'] = time();
            $res = up_one("users_jiinfo","id = {$pan['id']}",$data);
        }else{
            $data['user_id']   = $user['id'];
            $data['create_at'] = time();
            $res = ins("users_jiinfo",$data);
        }
        if($res){echo 1;}else{echo 2;}
    }
    //背景
    public function back_img(){
        $code  = I('code');
        $user  = users("users","back_img",$code);
        $info = sels("users_backimg","img,id");
        if($info){
            foreach ($info as $k=>$b){
                $info[$k]['img']    = IMGS.$b['img'];
                if($user['back_img'] == $b['id']){$info[$k]['choose'] = true;}else{$info[$k]['choose'] = false;}
            }
        }else{$info = 1;}
        echo json_encode(array("info"=>$info));
    }
    //用户更换背景
    public function huan_img(){
        $code  = I('code');
        $id    = I('id');
        $user  = users("users","id",$code);
        $res   = up_one("users","id = {$user['id']}",array("back_img"=>$id));
        if($res){echo 1;}else{echo 2;}
    }
    //我的收藏
    public function collectss(){
        $code = I('code');
        $user = users("users", "id", $code);
        $user_id      = $user['id'];
        $h            = sels("collect_goods","goods_id","user_id = {$user_id}");
        if($h){
            foreach($h as $k=>$v) {$goods_id[] = $v['goods_id'];}
            $goods_ids = implode(",",$goods_id);
            $goods     = sels("goods","id,banner,name,moneys",array("id"=>array("in",$goods_ids),"status"=>1),"id desc");
            $goods     = goods_xun($goods);
        }else{
            $goods = 1;//暂无收藏
        }
        echo json_encode(array("goods"=>$goods));
    }
    //我的好友
    public function collect(){
        $type = I('type'); //已查看  已关注 已浏览
        if($type == 1){$table = "collect_cha";}
        if($type == 2){$table = "collect_users";}
        if($type == 3){$table = "collect_liu";}
        $code = I('code');
        $user = users("users", "id", $code);
        $user_id      = $user['id'];
        $h            = sels($table,"goods_id","user_id = {$user_id}","id desc",6);
        if($h){
            foreach($h as $k=>$v) {$goods_id[] = $v['goods_id'];}
            $goods_ids = implode(",",$goods_id);
            $info = joins("users","a.id,a.flowers,b.avatar,b.user_id,b.nick_name,b.age,b.province,b.city",array("a left join __USERS_JIINFO__ b on a.id = b.user_id"),array("a.id"=>array("in",$goods_ids)));
            foreach ($info as $k=>$v){
                $age                = strtotime($v['age']);
                $info[$k]['ages']  = ages($age)."岁";
                $info[$k]['avatar']= IMGS.$v['avatar'];
                if(!$v['flowers']){ $info[$k]['flowers'] = 0;}
            }
        }else{
            $info = 1;//暂无收藏
        }
        echo json_encode(array("info"=>$info));
    }
    //下拉加载
    public function la()
    {
        $currpage = I('currpage');

        $type = I('type'); //已查看  已关注 已浏览
        if($type == 1){$table = "collect_cha";}
        if($type == 2){$table = "collect_users";}
        if($type == 3){$table = "collect_liu";}
        $code = I('code');
        $user = users("users", "id", $code);
        $user_id      = $user['id'];

        $filed   = "goods_id";
        $where   = "user_id = {$user_id}";
        $orders  = "id desc";
        $pagesize = 6;
        $h = currpage($table,$filed,$where,$pagesize,$currpage,$orders);
        if($h){
            foreach($h as $k=>$v) {$goods_id[] = $v['goods_id'];}
            $goods_ids = implode(",",$goods_id);
            $info = joins("users","a.id,a.flowers,b.avatar,b.user_id,b.nick_name,b.age,b.province,b.city",array("a left join __USERS_JIINFO__ b on a.id = b.user_id"),array("a.id"=>array("in",$goods_ids)));
            foreach ($info as $k=>$v){
                $age                = strtotime($v['age']);
                $info[$k]['ages']  = ages($age)."岁";
                $info[$k]['avatar']= IMGS.$v['avatar'];
                if(!$v['flowers']){ $info[$k]['flowers'] = 0;}
            }
        }else{
            $info = 1;//暂无收藏
        }
        echo json_encode(array("info"=>$info));
    }
    //我的消息
    public function information(){
        $code = I('code');
        $user = users("users", "id", $code);
        $type = I('type');//1已接收  2已申请
        if($type == 1){$where = "bei_id = {$user['id']} and b_del = 1";}
        if($type == 2){$where = "user_id = {$user['id']} and u_del = 1";}
        //我的消息
       $users_jiaoinfo = $this->tong($where,$type);
        echo json_encode(array("info"=>$users_jiaoinfo));
    }
    //删除我的消息
    public function del_information(){
        $code = I('code');
        $user = users("users", "id", $code);

        $type = I('type');//1已接收b  2已申请u
        $id = json_decode($_GET['id'], true);
        $ids = implode(",",$id);
        if($type == 1){
            $test = sels("users_jiaoinfo","id,b_del,u_del",array("id"=>array("in",$ids)));
            foreach ($test as $k=>$v){
                if($v['u_del'] == 2){
                    $del = del("users_jiaoinfo","id = {$v['id']}");
                }else{
                    $del = up_one("users_jiaoinfo","id = {$v['id']}",array("b_del"=>2));
                }
            }
        }
        if($type == 2){
            $test = sels("users_jiaoinfo","id,u_del,b_del",array("id"=>array("in",$ids)));
            foreach ($test as $k=>$v){
                if($v['b_del'] == 2){
                    $del = del("users_jiaoinfo","id = {$v['id']}");
                }else{
                    $del = up_one("users_jiaoinfo","id = {$v['id']}",array("u_del"=>2));
                }
            }
        }
        if($del){
            if($type == 1){$where = "bei_id = {$user['id']} and b_del = 1";}
            if($type == 2){$where = "user_id = {$user['id']} and u_del = 1";}
            $users_jiaoinfo = $this->tong($where,$type);
            echo json_encode(array("info"=>$users_jiaoinfo));
        }else{echo json_encode(array("info"=>2));}
    }
    public function tong($where,$type){
        $users_jiaoinfo = sels("users_jiaoinfo","",$where,"id desc");
        if(!$users_jiaoinfo){$users_jiaoinfo = 1;}else{
            foreach ($users_jiaoinfo as $k=>$v){
                $users_jiaoinfo[$k]['select'] = false;
                if($v['status'] == 1){
                    $users_jiaoinfo[$k]['create_at'] = date("Y-m-d",$v['create_at']);
                    if($type == 1){
                        $u = sel("users_jiinfo","nick_name,avatar","user_id = {$v['user_id']}");
                        $u_i = sel("users","id,nickname,headimgurl","id = {$v['user_id']}");
                    }
                    if($type == 2){
                        $u = sel("users_jiinfo","nick_name,avatar","user_id = {$v['bei_id']}");
                        $u_i = sel("users","id,nickname,headimgurl","id = {$v['bei_id']}");
                    }
                        if($u['nick_name']){
                            $users_jiaoinfo[$k]['nick_name'] = $u['nick_name'];
                        }else{
                            $users_jiaoinfo[$k]['nick_name'] = $u_i['nickname'];
                        }
                        if($u['avatar']){
                            $users_jiaoinfo[$k]['avatar'] = IMGS.$u['avatar'];
                        }else{
                            $users_jiaoinfo[$k]['avatar'] = $u_i['headimgurl'];
                        }

                }
                if($v['status'] == 2){
                    $users_jiaoinfo[$k]['up_at'] = date("Y-m-d",$v['up_at']);
                    if($type == 1){
                        $u = sel("users_jiinfo","nick_name,avatar","user_id = {$v['user_id']}");
                        $u_i = sel("users","id,nickname,headimgurl","id = {$v['user_id']}");
                    }
                    if($type == 2){
                        $u = sel("users_jiinfo","nick_name,avatar","user_id = {$v['bei_id']}");
                        $u_i = sel("users","id,nickname,headimgurl","id = {$v['bei_id']}");
                    }
                    if($u['nick_name']){
                        $users_jiaoinfo[$k]['nick_name'] = $u['nick_name'];
                    }else{
                        $users_jiaoinfo[$k]['nick_name'] = $u_i['nickname'];
                    }
                    if($u['avatar']){
                        $users_jiaoinfo[$k]['avatar'] = IMGS.$u['avatar'];
                    }else{
                        $users_jiaoinfo[$k]['avatar'] = $u_i['headimgurl'];
                    }
                }
                if($v['status'] == 3){
                    $users_jiaoinfo[$k]['up_at'] = date("Y-m-d",$v['up_at']);
                    if($type == 1){
                        $u = sel("users_jiinfo","nick_name,avatar,qq,wechat","user_id = {$v['user_id']}");
                        $u_i = sel("users","id,nickname,headimgurl","id = {$v['user_id']}");
                    }
                    if($type == 2){
                        $u = sel("users_jiinfo","nick_name,avatar,qq,wechat","user_id = {$v['bei_id']}");
                        $u_i = sel("users","id,nickname,headimgurl","id = {$v['bei_id']}");
                    }
                    if($u['nick_name']){
                        $users_jiaoinfo[$k]['nick_name'] = $u['nick_name'];
                    }else{
                        $users_jiaoinfo[$k]['nick_name'] = $u_i['nickname'];
                    }
                    if($u['avatar']){
                        $users_jiaoinfo[$k]['avatar'] = IMGS.$u['avatar'];
                    }else{
                        $users_jiaoinfo[$k]['avatar'] = $u_i['headimgurl'];
                    }
                    $users_jiaoinfo[$k]['qq'] = $u['qq'];
                    $users_jiaoinfo[$k]['wechat'] = $u['wechat'];
                }
            }
        }
        return $users_jiaoinfo;
    }
    //我的消息 用户确认
    public function que_information(){
        $id   = I('id');
        $type = I('type');// 2拒绝  3同意
        $res  = up_one("users_jiaoinfo","id = {$id}",array("status"=>$type,"up_at"=>time()));
        if($res){
            $user = sel("users_jiaoinfo","bei_id","id = {$id}");
            $where = "bei_id = {$user['bei_id']} and b_del = 1";
            $users_jiaoinfo = $this->tong($where,1);
            echo json_encode(array("info"=>$users_jiaoinfo));
        }else{
          echo 1;
        }
    }
    //优惠券
    public function coupons()
    {
        $code = I('code');
        $users = users("users", "id", $code);
        //仅展示没过期的
        $time = time();
        $test = sels("users_coupons", "coupons_id", "user_id = {$users['id']} and type = 1");
        if ($test) {
            foreach ($test as $k => $v) {
                $id[] = $v['coupons_id'];
            }
            $ids = implode(",", $id);
            $coupons = sels("coupons", "id,img,man,money,start_at,end_at", array("id" => array("in", $ids), "end_at" => array("gt", $time)));
            foreach ($coupons as $k => $v) {
                $coupons[$k]['start_at'] = date("Y/m/d", $v['start_at']);
                $coupons[$k]['end_at'] = date("Y/m/d", $v['end_at']);
                $coupons[$k]['select'] = false;
                $coupons[$k]['mask'] = 0;
                $coupons[$k]['masks'] = 0;
                $coupons[$k]['img']   = IMGS.$v['img'];
            }
            if ($coupons) {
                $list = $coupons;
            } else {
                echo 1;
            }
        } else {
            $list = 1;
        }
        echo json_encode($list);
    }
    //购物车
    public function carss()
    {
        $code = I('code');
        $user = users("users", "id,covers,coupons_id", $code);
        $cars = sels("cars", " * ", "user_id = {$user['id']}");
        if ($cars) {
            foreach ($cars as $k => $v) {
                $goods = sel("goods", "banner,name,lei,covers", "id = {$v['goods_id']}");
                $cover[]= $goods['covers'];
                //店铺名称
                //$sto                    = sel("stores","store_name","id = {$goods['stores_id']}");
                $cars[$k]['ids'] = $k;
                //$cars[$k]['store_name']= $sto['store_name'];
                $cars[$k]['store_name'] = "寻爱";
                $cars[$k]['shop_img']   = IMGS . $goods['banner'];
                $cars[$k]['shop_name']  = $goods['name'];
                $cars[$k]['shop_num']   = $v['nums'];
                $cars[$k]['selected']   = true;
                $cars[$k]['pan']         = 1;
                $cars[$k]['shop_money'] = $v['moneys'];
            }
            //总积分数 判断用户与总积分数是否相同
            $a = array_sum($cover);
            if($a > $user['covers']){
                $a = $user['covers'];
            }else{
                $a = $a;
            }
            //计算积分核算的钱数  nums_seven
            $covers_shu  = sel("covers_shu","nums_seven");
            $c_m         = floor($covers_shu['nums_seven']*$a);
            if(!$c_m){$a = 0;}
            $covers_moneys = $c_m;
        } else {
            $cars = 1;
        }
        //通过购物车总价算优惠券
        /* if($cars != 1){
             foreach ($cars as $k=>$v){
                 $moneys[] = $v['shop_money'];
             }
             $m = 0;
             for ($i=0;$i<count($moneys);$i++){
                 $m += $moneys[$i];
             }
             $zong = $m;

         }*/
        //仅展示没过期的
        if ($cars != 1) {
            $time = time();
            $test = sels("users_coupons", "coupons_id", "user_id = {$user['id']} and type = 1");
            if ($test) {
                foreach ($test as $k => $v) {
                    $id[] = $v['coupons_id'];
                }
                $ids     = implode(",", $id);
                $coupons = sels("coupons", "id,man,money,start_at,end_at", array("id" => array("in", $ids), "end_at" => array("gt", $time)));
                if ($coupons) {
                    foreach ($coupons as $k => $v) {
                        $coupons[$k]['start_at'] = date("Y/m/d", $v['start_at']);
                        $coupons[$k]['end_at'] = date("Y/m/d", $v['end_at']);
                    }
                    $list = $coupons;
                } else {$list = 1;}
            } else {$list = 1;}
        }
        if ($user['covers'] < 1) {$user['covers'] = "0";}
        echo json_encode(array("info" => $cars,"covers" => $a,"covers_moneys"=>$covers_moneys, "user_id" => $user['id'], "you" => $list, "xia_biao" => $user['coupons_id']));
        //echo json_encode(array("cover"=>$cover,"info" => $cars, "covers" => $user['covers'], "user_id" => $user['id'], "you" => $list, "xia_biao" => $user['coupons_id']));
    }
    //产品状态 订单状态  订单状态  1待付款 2待发货 3 待收货 4待评论 5已完成（数据库中得订单状态值）
    public function orderss(){
        $type    = I('type');
        $code    = I('code');
        $user    = users("users","id",$code);
        if($type == 1 || $type == "undefined"){ //待付款
            $wh  = "status = 1";
            $gou = 1;
        }
        if($type == 2){ //待发货
            $wh  = "status = 2";
            $gou = 2;
        }
        if($type == 3){ //待收货
            $wh = "status = 3";
            $gou = 3;
        }
        if($type == 4){ //待评价
            $wh  = "status = 4";
            $gou = 4;
        }
        if($type == 5){ //已完成
            $wh  = "status = 5";
            $gou = 5;
        }
        $h = sels("orders","id,goods_id,names,numbers,moneys,nums","type = 1 and del = 1 and user_id = {$user['id']} and trade_no != '' and {$wh}","id desc");
        if($h){
            foreach($h as $k=>$v) {
                $shang                 = sel("goods","id,banner,name","id = {$v['goods_id']}");
                $h[$k]['store_name']  = "寻爱";
                $h[$k]['banner']       = IMGS . $shang['banner'];
                $h[$k]['od']           = $gou;
                $h[$k]['name']        = $shang['name'];
            }
        }else{$h = 1;}
        echo json_encode(array("info"=>$h));
    }
    //已完成//待付款的删除
    public function orders_del(){
        $id   = I("id");
        $type = I('type');
        $user = sel("orders","user_id","id = {$id}");

        if($type == 1){  //待付款删除 1
            $wh  = "status = 1";
            $gou = 1;
            $res = del("orders","id = {$id}");
        }
        if($type == 5){  //已完成删除
            $wh  = "status = 5";
            $gou = 5;
            $res = up_one("orders","id = {$id}",array("del"=>2));
        }
        if($res){
            $h = sels("orders","id,goods_id,names,numbers,moneys,nums","type = 1 and del = 1 and user_id = {$user['user_id']} and trade_no != '' and {$wh}","id desc");
            if($h){
                foreach($h as $k=>$v) {
                    $shang                 = sel("goods","id,banner,name","id = {$v['goods_id']}");
                    $h[$k]['store_name']  = "寻爱";
                    $h[$k]['banner']       = IMGS . $shang['banner'];
                    $h[$k]['od']           = $gou;
                    $h[$k]['name']        = $shang['name'];
                }
            }else{$h = 1;}
            echo json_encode(array("info"=>$h));
        }else{
            echo 2;
        }
    }
    //确认完成订单
    public function que(){
        $orders_id = I('id');
        $res       = up_one("orders","id = {$orders_id}",array("status"=>4));
        if($res){
            $user= sel("orders","user_id,moneys","id = {$orders_id}");
            //用户完成订单  获得积分
            $covers_shu = sel("covers_shu","nums_two");
            $covers_uss = sel("users","covers","id = {$user['user_id']}");
            $orders_fen = floor($user['moneys'])*$covers_shu['nums_two']+$covers_uss['covers'];
            $u_covers   = up_one("users","id = {$user['user_id']}",array("covers"=>$orders_fen));
            $wh  = "status = 3";
            $gou = 3;
            $h = sels("orders","id,goods_id,names,numbers,moneys,nums","type = 1 and del = 1 and user_id = {$user['user_id']} and trade_no != '' and {$wh}","id desc");
            if($h){
                foreach($h as $k=>$v) {
                    $shang                 = sel("goods","id,banner,name","id = {$v['goods_id']}");
                    $h[$k]['store_name']  = "寻爱";
                    $h[$k]['banner']       = IMGS . $shang['banner'];
                    $h[$k]['od']           = $gou;
                    $h[$k]['name']         = $shang['name'];
                }
            }else{$h = 1;}
        }else{$h = 2;}
        echo json_encode(array("info"=>$h));
    }
    //订单评价
    public function pings()
    {
        $id = I('orders_id');//订单id
        $content = I('content');
        if ($content) {
            $goods = sel("orders", "user_id,goods_id", "id = {$id}");
            $orders = up_one("orders", array("id" => $id), array("status" => 5));
            if ($orders) {
                $data['orders_id'] = $id;
                $data['user_id'] = $goods['user_id'];
                $data['content'] = $content;
                $data['goods_id'] = $goods['goods_id'];
                $data['create_at'] = time();
                $data['xing_one'] = I("one");
                $data['xing_two'] = I("two");
                $data['status'] = I('status');
                $res = ins("comment_goods", $data);
                echo 1;
            } else {
                echo 2;
            }
        } else {
            echo 3;
        }
    }
    //用户意见反馈 (用户完善信息后才可以反馈)
    public function fan(){
        $user_id = I('id');
        $res     = sel("users","phone,name","id = {$user_id}");
        if($res['phone'] && $res['name']){
            echo 1;
        }else{
            echo 2;//未完善信息
        }
    }
    public function backs(){
        $content            = I('content');
        if (preg_match("/[\x7f-\xff]/", $content)) {
            $data['content']   = $content;
        } else {
            $data['content']   = false;
        }
        if($data['content']){
            $data['create_at'] = time();
            $data['user_id']   = I('id');
            $res = ins("backs",$data);
            if($res){
                echo 2;
            }else{
                echo 3;
            }
        }else{
            echo 1;//请输入合理的反馈信息
        }
    }
    //关于我们
    public function about_us(){
        $res = sel("company");
        $res['one'] = IMGS.$res['one'];
        $res['two'] = IMGS.$res['two'];
        $res['three'] = IMGS.$res['three'];
        echo json_encode(array("info"=>$res));
    }
    //服务地址
    public function info_address(){
        $user_id     = I('user_id');
        $res         = sels("address","","user_id = {$user_id}");
        if(!$res){$res = 1;}
        echo json_encode(array("info"=>$res));
    }
    public function add_address(){
        $data['user_id']    = I('user_id');
        $data['name']       = I('name');
        $data['phone']      = I('phone');
        $data['address']    = I('address');
        $data['create_at']  = time();
        $res                 = ins("address",$data);
        if($res){echo 1;}else{echo 2;}
    }
    //购物车选择优惠券
    public function gou_you(){
        $code                = I('code');
        $users               = users("users","id,coupons_id,goods_id",$code);
        $xia_biao            = I('id');//用户选择变更优惠券
        $data['coupons_id'] = $xia_biao;
        $goods_id             = I('goods_id');
        if($goods_id){$data['goods_id'] = $goods_id;}
        if($users['goods_id']){
            if($users['coupons_id'] == $xia_biao && $users['goods_id'] == $goods_id){
                $up       = true;
            }else{
                $up       = up_one("users","id = {$users['id']}",$data);
            }
        }else{
            if($users['coupons_id'] == $xia_biao){
                $up       = true;
            }else{
                $up       = up_one("users","id = {$users['id']}",$data);
            }
        }
        if($up){$a = 1;}else{$a = 2;}
        echo json_encode(array("info"=>$a,"xia_biao"=>$xia_biao));
    }
    //交换qq/微信
    public function info_qq(){
        $type    = I('type'); //1已接收   2已申请
        $user_id = I('user_id');  //用户id
        if($type == 1){$res     = sels("users_jiaoinfo","","bei_id  = {$user_id}");}
        if($type == 2){$res     = sels("users_jiaoinfo","","user_id = {$user_id}");}
        if($res){
            foreach ($res as $k=>$v){
                if($type == 2 && $v['status'] == 3){
                    $us = sel("users_jiinfo","qq,wechat","user_id = {$v['bei_id']}");
                    $res[$k]['qq']     = $us['qq'];
                    $res[$k]['wechat'] = $us['wechat'];
                }
            }
        }else{
            echo 2;  //无信息
        }
    }
    //图片 视频 上传
    public function tu(){
        $img     = I('avatar');
        $img_one  = I('img_one');
        $img_two  = I('img_two');
        $img_three = I('img_three');
        $img_four  = I('img_four');
        $vedio     = I('vedio');
        if (!empty($_FILES['one']['name'])) {
            unlink($img);
            $info  = $this->imgUpload("users/");
            $url   = 'Data/' . $info['one']['savepath'] . $info['one']['savename'];
        }
        if (!empty($_FILES['two']['name'])) {
            unlink($img_one);
            $info  = $this->imgUpload("users/");
            $url   = 'Data/' . $info['two']['savepath'] . $info['two']['savename'];
        }
        if (!empty($_FILES['three']['name'])) {
            unlink($img_two);
            $info  = $this->imgUpload("users/");
            $url   = 'Data/' . $info['three']['savepath'] . $info['three']['savename'];
        }
        if (!empty($_FILES['four']['name'])) {
            unlink($img_three);
            $info  = $this->imgUpload("users/");
            $url   = 'Data/' . $info['four']['savepath'] . $info['four']['savename'];
        }
        if (!empty($_FILES['five']['name'])) {
            unlink($img_four);
            $info  = $this->imgUpload("users/");
            $url   = 'Data/' . $info['five']['savepath'] . $info['five']['savename'];
        }
        if (!empty($_FILES['six']['name'])) {
            unlink($vedio);
            $info  = $this->imgUpload("users/");
            $url   = 'Data/' . $info['six']['savepath'] . $info['six']['savename'];
        }
        echo $url;
    }
    //用户没有保存图片信息 删除
    public function del_tu(){
        $avatar    = I('avatar');
        $img_one   = I('img_one');
        $img_two   = I('img_two');
        $img_three = I('img_three');
        $img_four  = I('img_four');
        $vedio     = I('vedio');
        unlink($avatar);unlink($img_one);unlink($img_two);unlink($img_three);unlink($img_four);unlink($vedio);
    }
    //头像 / 相册 修改 保存
    public function avatar_img(){
        $code                = I('code');
        $users               = users("users","id",$code);
        $ji                  = sel("users_jiinfo","id,img,avatar,vedio","user_id = {$users['id']}");
        $avatar    = I('avatar');
        $img_one   = I('img_one');
        $img_two   = I('img_two');
        $img_three = I('img_three');
        $img_four  = I('img_four');
        $vedio     = I('vedio');
        $data['user_id'] = $users['id'];
        if($ji['img']){$imgs = explode(",",$ji['img']);}else{$imgs = 1;}
        if($avatar){
            unlink($ji['avatar']);
            $data['avatar'] = $avatar;
        }
        if($vedio){
            unlink($ji['vedio']);
            $data['vedio'] = $vedio;
        }
        if($img_one){
            unlink($imgs[0]);
            $img[0] = $img_one;
        }else{
            if($imgs != 1){
                $img[0] = $imgs[0];
            }else{
                $img[0] = "";
            }
        }
        if($img_two){
            unlink($imgs[1]);
            $img[1] = $img_two;
        }else{
            if($imgs != 1){
                $img[1] = $imgs[1];
            }else{
                $img[1] = "";
            }
        }
        if($img_three){
            unlink($imgs[2]);
            $img[2] = $img_three;
        }else{
            if($imgs != 1){
                $img[2] = $imgs[2];
            }else{
                $img[2] = "";
            }
        }
        if($img_four){
            unlink($imgs[3]);
            $img[3] = $img_four;
        }else{
            if($imgs != 1){
                $img[3] = $imgs[3];
            }else{
                $img[3] = "";
            }
        }
        $data['img'] = implode(",",$img);
        if($ji){
            $data['up_at'] = time();
            $res = up_one("users_jiinfo","id = {$ji['id']}",$data);
        }else{
            $data['create_at'] = time();
            $res = ins("users_jiinfo",$data);
        }
        if($res){echo 1;}else{echo 2;}
    }
    //我的转介
    public function zhuan(){
        $type       = I('type');   //1已消费   2为消费
        if($type == 1){$where = "a.status = 2";}
        if($type == 2){$where = "a.status = 1";}
        $code       = I('code');
        $user       = users("users","id,headimgurl,nickname",$code);
        //已完成到店消费
        $res        = joins("zhuan","a.zhuan_id,a.id,a.create_at,b.nickname,b.headimgurl",array("a left join __USERS__ b on a.zhuan_id = b.id"),"a.user_id = {$user['id']} and {$where}");
        if($res){
            foreach($res as $k=>$v){
                if($type == 1){
                    $res[$k]['zhuan']     = xiaos($v['zhuan_id']);
                }
                $res[$k]['create_at']  = date("Y-m-d",$v['create_at']);
                $res[$k]['pan']        = "0";
            }
        }else{$res = 1;}
        echo json_encode(array("info"=>$res,"user"=>$user));
    }
    //转介的删除
    public function zhuan_del(){
        $id      = I('id');
        $user_id = I('user_id');
        $res     = del("zhuan","id = {$id}");
        if($res){
            $type    = I('type')+1;
            if($type == 1){$status = 2;}//已完成消费
            if($type == 2){$status = 1;}//未完成消费
            $res        = joins("zhuan","a.id,a.create_at,b.nickname,b.headimgurl,b.phone",array("a left join __USERS__ b on a.zhuan_id = b.id"),"a.user_id = {$user_id} and a.status = {$status}");
            if($res){
                foreach($res as $k=>$v){
                    $res[$k]['create_at'] = date("Y-m-d",$v['create_at']);
                    $res[$k]['pan']        = "1";
                }
            }else{$res = 1;}
            echo json_encode(array("info"=>$res,"user_id"=>$user_id));
        }else{echo 2;}
    }
    //协议
    public function xie_rules(){
        $type = I('type');// 1分销说明  2签到说明  3转接规则
        $res  = sel("xie","content","status = {$type}");
        echo json_encode(array("info"=>$res['content']));
    }
    //鲜花记录
    public function flowers(){
        $code = I('code');
        $user = users("users","id",$code);
        $orders = sels("orders","id,user_id,nums,create_at","type = 4 and goods_id = {$user['id']} and del = 1");
        if($orders){
            foreach ($orders as $k=>$v){
                $orders[$k]['select'] = false;
                $orders[$k]['create_at'] = date("Y-m-d H:i",$v['create_at']);
                $song = join_one("users","a.nickname,a.headimgurl,b.nick_name,b.name",array("a left join __USERS_JIINFO__ b on a.id = b.user_id"),"a.id = {$v['user_id']}");
                if($song['nick_name']){$orders[$k]['nickname'] = $song['nick_name'];}
                if(!$song['avatar']){$orders[$k]['avatar'] = $song['headimgurl'];}else{$orders[$k]['avatar'] = IMGS.$song['avatar'];}
            }
        }else{$orders = 1;}
        echo json_encode(array("info"=>$orders));
    }
    public function del_flowers(){
        $id  = json_decode($_GET['id'], true);
        $ids = implode(",",$id);
        $del = up_one("orders",array("id"=>array("in",$ids)),array("del"=>2));
        if($del){
            $code = I('code');
            $user = users("users","id",$code);
            $orders = sels("orders","id,user_id,nums,create_at","type = 4 and goods_id = {$user['id']} and del = 1");
            if($orders){
                foreach ($orders as $k=>$v){
                    $orders[$k]['select'] = false;
                    $orders[$k]['create_at'] = date("Y-m-d H:i",$v['create_at']);
                    $song = join_one("users","a.nickname,a.headimgurl,b.nick_name,b.name",array("a left join __USERS_JIINFO__ b on a.id = b.user_id"),"a.id = {$v['user_id']}");
                    if($song['nick_name']){$orders[$k]['nickname'] = $song['nick_name'];}
                    if(!$song['avatar']){$orders[$k]['avatar'] = $song['headimgurl'];}else{$orders[$k]['avatar'] = IMGS.$song['avatar'];}
                }
            }else{$orders = 1;}
            echo json_encode(array("info"=>$orders));
        }else{echo 2;}
    }
    //单图上传  视频上传
    protected function imgUpload($path)
    {
        $config = array(
            'maxSize' => 3145728,
            'rootPath' => 'Data/',
            'savePath' => $path,
            'saveName' => time() . '_' . mt_rand(),
            'exts' => array('jpg', 'png', 'jpeg', 'gif','mp4','wmv'),
            'autoSub' => true,
            'subName' => array('date', 'Ymd')
        );
        $upload = new \Think\Upload($config);
        $info = $upload->upload();
        if (!$info) {
            $this->error($upload->getError());
        }
        return $info;
    }
    //生成缩略图
    protected function thumbImg($path, $thumbName, $width = 650, $height = 400)
    {
        $image = new \Think\Image();
        $image->open($path);
        $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_CENTER)->save($thumbName);
    }
}